<template>
  <!-- SKU管理 -->
  <el-card>
    <el-table border style="margin-bottom: 15px;" :data="skuArr">
      <el-table-column label="序号" type="index" align="center" width="80px">
      </el-table-column>
      <el-table-column label="名称" show-overflow-tooltip prop="skuName">
      </el-table-column>
      <el-table-column label="描述" show-overflow-tooltip prop="skuDesc">
      </el-table-column>
      <el-table-column label="图片">
        <template #="{ row, $index }">
          <img :src="row.skuDefaultImg" alt="" width="150px">
        </template>
      </el-table-column>
      <el-table-column label="重量" prop="weight">
      </el-table-column>
      <el-table-column label="价格" prop="price">
      </el-table-column>
      <el-table-column label="操作" fixed="right" width="220px">
        <template #="{ row, $index }">
          <el-button :type="row.isSale == 1 ? 'info' : 'success'" :icon="row.isSale == 1 ? 'Bottom' : 'Top'"
            @click="updateSale(row)" size="small"></el-button>
          <el-button type="primary" icon="Edit" size="small" @click="updateSku"></el-button>
          <el-button type="info" icon="InfoFilled" size="small" @click="findSku(row)"></el-button>
          <el-popconfirm :title="`你确定要删除${row.skuName}?`" @confirm="removeSku(row.id)">
            <template #reference>
              <el-button type="danger" icon="Delete" size="small"></el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页器 -->
    <el-pagination v-model:current-page="pageNo" v-model:page-size="pageSize" :page-sizes="[3, 5, 7, 9]"
      :background="true" layout="jumper, prev, pager, next, ->, total, sizes" @current-change="getHasSku"
      @size-change="getHasSku(1)" :total="total" />
    <!-- 展示SKU全部信息的抽屉 -->
    <el-drawer v-model="drawer">
      <template #header>
        <h4>查看商品详情</h4>
      </template>
      <template #default>
        <!-- 名称 -->
        <el-row>
          <el-col :span="6">名称</el-col>
          <el-col :span="18">{{ skuInfo.skuName }}</el-col>
        </el-row>
        <!-- 描述 -->
        <el-row>
          <el-col :span="6">描述</el-col>
          <el-col :span="18">{{ skuInfo.skuDesc }}</el-col>
        </el-row>
        <!-- 价格 -->
        <el-row>
          <el-col :span="6">价格</el-col>
          <el-col :span="18">{{ skuInfo.price }}</el-col>
        </el-row>
        <!-- 平台属性 -->
        <el-row>
          <el-col :span="6">平台属性</el-col>
          <el-col :span="18">
            <el-tag type="primary" v-for="item in skuInfo.skuAttrValueList" :key="item.attrId" :title="item.attrName">{{
              item.valueName }}</el-tag>
          </el-col>
        </el-row>
        <!-- 销售属性 -->
        <el-row>
          <el-col :span="6">销售属性</el-col>
          <el-col :span="18">
            <el-tag type="success" v-for="item in skuInfo.skuSaleAttrValueList" :key="item.saleAttrId"
              :title="item.saleAttrName">{{ item.saleAttrValueName }}</el-tag>
          </el-col>
        </el-row>
        <!-- 商品图片 -->
        <el-row>
          <el-col :span="6">商品图片</el-col>
          <el-col :span="18">
            <div v-if="skuInfo.skuImageList.length > 0">暂无图片</div>
            <el-carousel v-else interval="2000" type="card" height="200px" indicator-position="outside" autoplay>
              <el-carousel-item v-for="item in skuInfo.skuImageList" :key="item.id">
                <img :src="item.imgUrl" alt="" width="100%" height="100%" />
              </el-carousel-item>
            </el-carousel>
          </el-col>
        </el-row>
      </template>
    </el-drawer>
  </el-card>
</template>

<script setup lang="ts">
import { onMounted, ref } from 'vue';
import { reqCancelSku, reqSaleSku, reqSkuList, reqSkuInfo, reqRemoveSku } from '@/api/product/sku';
import { skuData, SkuResponseData } from '@/api/product/sku/type';
import { ElMessage } from 'element-plus';

//#region 数据定义
// 分页器相关数据
let pageNo = ref<number>(1);
let pageSize = ref<number>(5);
let total = ref<number>(0);
// 抽屉相关数据
let drawer = ref<boolean>(false)
// 所有商品sku信息数组
let skuArr = ref<skuData[]>([])
// 某个商品sku数组
let skuInfo = ref<skuData>({})
//#endregion 

onMounted(() => {
  getHasSku()
})

// 获得所有商品SKU数据
const getHasSku = async (pager = 1) => {
  pageNo.value = pager
  let result: SkuResponseData = await reqSkuList(pageNo.value, pageSize.value)
  if (result.code == 200) {
    total.value = result.data.total
    skuArr.value = result.data.records
  }
}

// 上架或下架某个商品
const updateSale = async (row: skuData) => {
  if (row.isSale == 1) {
    await reqCancelSku((row.id as number))
    ElMessage({ type: 'success', message: '下架成功' })
    getHasSku(pageNo.value)
  } else {
    await reqSaleSku((row.id as number))
    ElMessage({ type: 'success', message: '上架成功' })
    getHasSku(pageNo.value)
  }
}

// 更新已有SKU
const updateSku = () => {
  ElMessage({
    type: 'success',
    message: '此功能正在开发中'
  })
}

// 查看商品详情按钮的回调
const findSku = async (row: skuData) => {
  drawer.value = true
  let result = await reqSkuInfo((row.id as number))
  skuInfo.value = result.data
}

// 删除某一个已有商品
const removeSku = async (id: number) => {
  let result = await reqRemoveSku(id)
  if (result.code == 200) {
    ElMessage({ type: 'success', message: '删除成功' })
    getHasSku(skuArr.value.length > 1 ? pageNo.value : pageNo.value - 1)
  } else {
    ElMessage({ type: 'error', message: '删除失败' })
  }
}
</script>

<style scoped>
.el-row {
  margin-bottom: 20px;
}

.el-tag {
  margin-right: 10px;
}

.el-carousel__item:nth-child(2n) {
  background-color: #99a9bf;
}

.el-carousel__item:nth-child(2n + 1) {
  background-color: #d3dce6;
}
</style>